package cn.tuning.utils;

import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author lixiaolong
 * @date 2021-08-03 21:15
 * @description: 加密工具类
 */
public class MD5Util {

    public static final int time = 5;

    public static final String SALT = "springsecurity";

    /**
     * 密码加密方法
     *
     * @param password
     * @return
     */
    public static String encode(String password) {
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalStateException("MD5 algorithm not available.  Fatal (should be in the JDK).");
        }
        for (int i = 0; i < time; i++) {
            byte[] bytes = digest.digest((password + SALT).getBytes(StandardCharsets.UTF_8));
            password = String.format("%032x", new BigInteger(1, bytes));
        }
        return password;
    }

    public static void main(String[] args) {
        System.out.println(MD5Util.encode("1234"));

    }

}
